package com.hust360.percent.pojo;

import java.util.ArrayList;
import java.util.List;

import com.hust360.percent.PercentApplication;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ShopDAOImpl implements ShopDAO {

	PercentApplication application;

	public ShopDAOImpl(Application application) {
		this.application = (PercentApplication) application;
	}

	/**
	 * 
	 * @param shopId
	 * @return 找到时返回一个shop实例，失败时返回null
	 */
	@Override
	public Shop findById(Long shopId) {

		if (shopId == null) {
			return null;
		}

		Shop shop = null;
		Cursor cursor = application.getLocalDB().query(Shop.TABLE_NAME,
				Shop.COLUMNS, Shop.SHOPID_COLUMN + "=" + shopId, null, null,
				null, null);
		if (cursor.moveToFirst()) {
			shop = new Shop();
			shop.setShopId(cursor.getLong(cursor
					.getColumnIndex(Shop.SHOPID_COLUMN)));
			shop.setAddress(cursor.getString(cursor
					.getColumnIndex(Shop.ADDRESS_COLUMN)));
			shop.setBrand(cursor.getString(cursor
					.getColumnIndex(Shop.BRAND_COLUMN)));
			shop.setLogoPath(cursor.getString(cursor
					.getColumnIndex(Shop.LOGO_PATH_COLUMN)));
			shop.setShopName(cursor.getString(cursor
					.getColumnIndex(Shop.SHOPNAME_COLUMN)));
			shop.setTelephone(cursor.getString(cursor
					.getColumnIndex(Shop.TELEPHONE_COLUMN)));
			shop.setTotalRate(cursor.getFloat((cursor
					.getColumnIndex(Shop.TOTAL_RATE_COLUMN))));
			shop.setLongitude(cursor.getDouble(cursor
					.getColumnIndex(Shop.LONGITUDE_COLUMN)));
			shop.setLatitude(cursor.getDouble(cursor
					.getColumnIndex(Shop.LATITUDE_COLUMN)));
			shop.setCityId(cursor.getLong(cursor.getColumnIndex(Shop.CITY_ID)));
		}
		return shop;
	}

	/**
	 * 
	 * @param example
	 * @return 返回一个shop的list集合，返回值总不为null，失败时返回一个空的list
	 */
	@Override
	public List<Shop> findByExample(Shop example) {

		Shop shop = null;
		String shopId = example.getShopId() == null ? "" : Shop.SHOPID_COLUMN
				+ "=" + "'" + example.getShopId() + "'";
		String shopAddress = example.getAddress() == null ? ""
				: Shop.ADDRESS_COLUMN + "=" + "'" + example.getAddress() + "'";
		String shopBrand = example.getBrand() == null ? "" : Shop.BRAND_COLUMN
				+ "=" + "'" + example.getBrand() + "'";
		String shopLogoPath = example.getLogoPath() == null ? ""
				: Shop.LOGO_PATH_COLUMN + "=" + "'" + example.getLogoPath()
						+ "'";
		String shopName = example.getShopName() == null ? ""
				: Shop.SHOPNAME_COLUMN + "=" + "'" + example.getShopName()
						+ "'";
		String shopTelephone = example.getTelephone() == null ? ""
				: Shop.TELEPHONE_COLUMN + "=" + "'" + example.getTelephone()
						+ "'";
		String shopTotalRate = example.getTotalRate() == null ? ""
				: Shop.TOTAL_RATE_COLUMN + "=" + "'" + example.getTotalRate()
						+ "'";
		String shopLongitude = example.getLongitude() == null ? ""
				: Shop.LONGITUDE_COLUMN + "=" + "'" + example.getLongitude()
						+ "'";
		String shopLatitude = example.getLatitude() == null ? ""
				: Shop.LATITUDE_COLUMN + "=" + "'" + example.getLatitude()
						+ "'";
		String cityId = example.getCityId() == null ? "" : Shop.CITY_ID + "="
				+ "'" + example.getCityId() + "'";

		String selection = shopId;
		selection += shopId.equals("") || shopAddress.equals("") ? shopAddress
				: " AND " + shopAddress;
		selection += shopAddress.equals("") || shopBrand.equals("") ? shopBrand
				: " AND " + shopBrand;
		selection += shopBrand.equals("") || shopLogoPath.equals("") ? shopLogoPath
				: " AND " + shopLogoPath;
		selection += shopLogoPath.equals("") || shopName.equals("") ? shopName
				: " AND " + shopName;
		selection += shopName.equals("") || shopTelephone.equals("") ? shopTelephone
				: " AND " + shopTelephone;
		selection += shopTelephone.equals("") || shopTotalRate.equals("") ? shopTotalRate
				: " AND " + shopTotalRate;
		selection += shopTotalRate.equals("") || shopLongitude.equals("") ? shopLongitude
				: " AND " + shopLongitude;
		selection += shopLongitude.equals("") || shopLatitude.equals("") ? shopLatitude
				: " AND " + shopLatitude;
		selection += shopLatitude.equals("") || cityId.equals("") ? cityId
				: " AND " + cityId;

		selection = selection.equals("") ? null : selection;

		System.out.println(" the selection is " + selection);

		Cursor cursor = application.getLocalDB().query(Shop.TABLE_NAME, null,
				selection, null, null, null, null);
		List<Shop> list = new ArrayList<Shop>(cursor.getCount());
		while (cursor.moveToNext()) {
			shop = new Shop();
			shop.setShopId(cursor.getLong(cursor
					.getColumnIndex(Shop.SHOPID_COLUMN)));
			shop.setAddress(cursor.getString(cursor
					.getColumnIndex(Shop.ADDRESS_COLUMN)));
			shop.setBrand(cursor.getString(cursor
					.getColumnIndex(Shop.BRAND_COLUMN)));
			shop.setLogoPath(cursor.getString(cursor
					.getColumnIndex(Shop.LOGO_PATH_COLUMN)));
			shop.setShopName(cursor.getString(cursor
					.getColumnIndex(Shop.SHOPNAME_COLUMN)));
			shop.setTelephone(cursor.getString(cursor
					.getColumnIndex(Shop.TELEPHONE_COLUMN)));
			shop.setTotalRate(cursor.getFloat(cursor
					.getColumnIndex(Shop.TOTAL_RATE_COLUMN)));
			shop.setLongitude(cursor.getDouble(cursor
					.getColumnIndex(Shop.LONGITUDE_COLUMN)));
			shop.setLatitude(cursor.getDouble(cursor
					.getColumnIndex(Shop.LATITUDE_COLUMN)));
			shop.setCityId(cursor.getLong(cursor.getColumnIndex(Shop.CITY_ID)));
			list.add(shop);
		}
		return list;
	}

	/**
	 * 向数据库中插入数据的方法
	 */
	@Override
	public void save(Shop shop) {
		ContentValues values = new ContentValues();
		values.put(Shop.SHOPID_COLUMN, shop.getShopId());
		values.put(Shop.SHOPNAME_COLUMN, shop.getShopName());
		values.put(Shop.BRAND_COLUMN, shop.getBrand());
		values.put(Shop.ADDRESS_COLUMN, shop.getAddress());
		values.put(Shop.TOTAL_RATE_COLUMN, shop.getTotalRate());
		values.put(Shop.TELEPHONE_COLUMN, shop.getTelephone());
		values.put(Shop.LOGO_PATH_COLUMN, shop.getLogoPath());
		values.put(Shop.LATITUDE_COLUMN, shop.getLatitude());
		values.put(Shop.LONGITUDE_COLUMN, shop.getLongitude());
		values.put(Shop.CITY_ID, shop.getCityId());
		synchronized (application.getLocalDB()) {
			application.getLocalDB().insertWithOnConflict(Shop.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
		}
	}

	@Override
	public boolean deleteById(Long shopId) {
		return false;
	}

	@Override
	public Long deleteByExample(Shop example) {
		return null;
	}

	@Override
	public List<Shop> findByArgs(String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		
		List<Shop> list;
		
		Cursor c = application.getLocalDB().query(Shop.TABLE_NAME, columns, selection, selectionArgs, groupBy, having, orderBy);
		list = new ArrayList<Shop>(c.getCount());
		Shop shop;
		while (c.moveToNext()) {
			shop = new Shop();
			
			shop.setShopId(c.getLong(c
					.getColumnIndex(Shop.SHOPID_COLUMN)));
			shop.setAddress(c.getString(c
					.getColumnIndex(Shop.ADDRESS_COLUMN)));
			shop.setBrand(c.getString(c
					.getColumnIndex(Shop.BRAND_COLUMN)));
			shop.setLogoPath(c.getString(c
					.getColumnIndex(Shop.LOGO_PATH_COLUMN)));
			shop.setShopName(c.getString(c
					.getColumnIndex(Shop.SHOPNAME_COLUMN)));
			shop.setTelephone(c.getString(c
					.getColumnIndex(Shop.TELEPHONE_COLUMN)));
			shop.setTotalRate(c.getFloat(c
					.getColumnIndex(Shop.TOTAL_RATE_COLUMN)));
			shop.setLongitude(c.getDouble(c
					.getColumnIndex(Shop.LONGITUDE_COLUMN)));
			shop.setLatitude(c.getDouble(c
					.getColumnIndex(Shop.LATITUDE_COLUMN)));
			shop.setCityId(c.getLong(c.getColumnIndex(Shop.CITY_ID)));
			list.add(shop);
		}
		c.close();
		return list;
	}

	
}
